package utils.titles;

import com.google.common.base.Joiner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static utils.titles.Expression.Gender.*;

/**
 * Created by nuno-rafael-figueiredo on 11-07-2014.
 */
public class Expression {
    public static final String REGEX = "\\s*([m|f|n])\\.\\s*:\\s*(.+)\\((.+)\\)\\s*";
    private static final Pattern PATTERN = Pattern.compile(REGEX);

    enum Gender{
        MASCULINE, FEMININE
    }

    private final Gender gender;
    private final String singular;
    private final String plural;

    public Expression(String text) {
        Matcher matcher = PATTERN.matcher(text);
        if (!matcher.find())
            throw new RuntimeException(text);

        gender = matcher.group(1).equals("m") ? MASCULINE : FEMININE;
        String radical = matcher.group(2);
        String[] suffixes = matcher.group(3).split("\\|");
        switch (suffixes.length){
            case 0:
                singular = radical;
                plural = radical;
                break;
            case 1:
                singular = radical;
                plural = radical + suffixes[0];
                break;
            default:
                singular = radical + suffixes[0];
                plural = radical + suffixes[1];
                break;
        }
    }

    @Override
    public String toString() {
        return Joiner.on(", ").join(gender, singular, plural);
    }
}
